package com.ly.jdk8.lambda;
import java.util.*;

import java.util.List;
import java.util.function.*;

/**
 * @Classname LambdaFunctionInterface
 * @Description
 *  内置4大函数型接口
 *
 * Consumer<T> 消费接口
 *   void accept(T t);
 *
 * Supplier<T> 供机型接口
 *   T get();
 *
 * Function<T,R> 函数型接口
 *  R get(T t);
 *
 * Predicate<T> 断言型接口
 *  boolean test(T t);
 *
 * @Date 2020/9/26 16:15
 * @Author 冷心影翼
 */
public class LambdaFunctionInterface {

    public static void main(String[] args) {

        LambdaFunctionInterface.useConsumer(1000,it->System.out.println(it-500));
        LambdaFunctionInterface.useConsumer(1000,it->{
            it-=1000;
        });
        LambdaFunctionInterface.useSupplier(5,()->{
           return new Random().nextInt(10);
        }).forEach(it->System.out.println(it));
        System.out.println("------");
        System.out.println(LambdaFunctionInterface.useFunction("123", it -> it.length() + ""));
        LambdaFunctionInterface.usePredicate(new String[]{"123","456","213131"},it->it.length() == 3).forEach(System.out::println);
    }

    public static void useConsumer(double money,Consumer<Double> consumer) {
        consumer.accept(money);
    }

    /**
     * 参数n个整数，添加到集合
     */
    public  static List<Integer> useSupplier(int num, Supplier<Integer> supplier) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < num; i++ ) {
            Integer integer = supplier.get();
            list.add(integer);
        }
        return list;
    }

    public static String useFunction(String str, Function<String,String> function) {
        String res = function.apply(str);
        return res;
    }
    /**
     *  将满足条件的字符串添加到集合中
     */
    public static List<String> usePredicate(String[] str, Predicate<String> predicate) {
        List<String> list = new ArrayList<>();
        for (String s:str) {
            if(predicate.test(s)) {
                list.add(s);
            }
        }
        return list;
    }

}
